/*LICENSE_BEGIN
 * ******************************************************************
 * All original parts of SoccerScope2003 have copyright (2003) by
 * Team YowAI, represented by Mr. Shuhei Shiota
 * The Department of Creative Informatics
 * The Graduate School of Information Science and Technology
 * The University of Tokyo
 * 
 * Additional classes (except libraries in /lib) were developed by 
 * the Dainamite Robocup-Team of the DAI-Labor, TU-Berlin 
 * (www.dai-labor.de)
 * 
 * Extended/new classes/packages are:
 *   soccerscope.analyzer.LogfileAnalyzer
 *   soccerscope.daistatistic.*
 *   soccerscope.daistatistic.charts.*
 *   soccerscope.daistatistic.tools.*
 *   soccerscope.db.*
 *   soccerscope.dialog.*
 *   soccerscope.model.PlayerWorldModel
 *   soccerscope.model.SimplePlayer
 *   soccerscope.net.AgentConnector
 *   soccerscope.training.*
 *   soccerscope.training.conditions.*
 * ******************************************************************
LICENSE_END*/
package soccerscope.view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;

import javax.swing.JButton;
import javax.swing.border.EtchedBorder;

import soccerscope.model.Param;
import soccerscope.model.Player;
import soccerscope.util.Color2;

public class PlayerButton extends JButton {

	private static final long serialVersionUID = 6600354325857492040L;

	private Player player;

	private FontMetrics fm;

	private int playerR;

	private int player2R;

	public PlayerButton(Player player) {
		super();
		this.player = player;
		this.playerR = (int) (player.getSize() * 3.6 * 8.0);
		this.player2R = this.playerR * 2;
		setBorder(new EtchedBorder(EtchedBorder.LOWERED));
		setBorderPainted(true);
	}

	public void setPlayer(Player player) {
		this.player = player;
		repaint();
	}

	@Override
	public Dimension getPreferredSize() {
		return getMinimumSize();
	}

	@Override
	public Dimension getMinimumSize() {
		return new Dimension(70, 40);
	}

	@Override
	public void paintComponent(Graphics g) {
		if (this.fm == null) {
			this.fm = this.getFontMetrics(this.getFont());
		}
		Dimension d = getSize();

		g.setColor(Color2.forestGreen);
		g.fillRect(0, 0, d.width, d.height);

		if (!this.player.isEnable())
			return;

		Font f = g.getFont();
		if (f.isBold()) {
			g.setFont(f.deriveFont(Font.PLAIN));
		}

		int cx = d.width / 2;
		int cy = d.height / 2;
		int x = cx - this.playerR;
		int y = cy - this.playerR;

		// fill back
		g.setColor(Color.black);
		g.fillOval(x, y, this.player2R, this.player2R);

		Color drawColor = this.player.getColor();
		if (this.player.isKicking()) {
			g.setColor(drawColor.darker());
			g.fillOval(x, y, this.player2R, this.player2R);
			g.setColor(Color.yellow);
			String s = "KICK";
			g.drawString(s, d.width - (this.fm.stringWidth(s) + 5), this.fm
					.getHeight());
		} else if (this.player.isCatching()) {
			g.setColor(drawColor.darker());
			g.fillOval(x, y, this.player2R, this.player2R);
			g.setColor(Color.yellow);
			String s = "CATCH";
			g.drawString(s, d.width - (this.fm.stringWidth(s) + 5), this.fm
					.getHeight());
		} else if (this.player.isTackling()) {
			g.setColor(drawColor.darker());
			g.fillOval(x, y, this.player2R, this.player2R);
			g.setColor(Color.yellow);
			String s = "TACKLE";
			g.drawString(s, d.width - (this.fm.stringWidth(s) + 5), this.fm
					.getHeight());
		}

		// body
		g.setColor(drawColor);
		g.drawOval(x, y, this.player2R, this.player2R);

		// angle_body
		g.fillArc(x, y, this.player2R, this.player2R,
				-((int) this.player.angle + 90), 180);

		// unum
		g.setColor(Color.white);
		g.drawString(Integer.toString(this.player.unum), 5,
				this.fm.getHeight() + 5);

		// stamina
		if (this.player.stamina > Param.STAMINA_MAX * Param.EFFORT_INC_THR) {
			g.setColor(Color.green);
		} else if (this.player.stamina > Param.STAMINA_MAX
				* Param.EFFORT_DEC_THR) {
			g.setColor(Color.yellow);
		} else {
			g.setColor(Color.red);
		}
		String s = Integer.toString(this.player.stamina);
		g.drawString(s, d.width - this.fm.stringWidth(s) - 5, d.height - 5);
	}
}
